﻿using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;

namespace Common
{
    public class ZipUtility
    {
        public static void Zipfiles(List<string> files, string downloadPath)
        {
            using (FileStream zip = File.Create(downloadPath))
            {
                using (ZipOutputStream ZipStream = new ZipOutputStream(zip))
                {
                    foreach (string file in files)
                    {
                        using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
                        {
                            ZipEntry zipEntry = new ZipEntry(Path.GetFileName(file));                            
                            ZipStream.PutNextEntry(zipEntry);
                            ZipStream.SetLevel(9);
                            byte[] buffer = new byte[1024];
                            int sizeRead = 0;
                            try
                            {
                                do
                                {
                                    sizeRead = fs.Read(buffer, 0, buffer.Length);
                                    ZipStream.Write(buffer, 0, sizeRead);
                                }
                                while (sizeRead > 0);
                            }
                            catch (Exception ex)
                            { throw ex; }
                            fs.Close();
                            fs.Dispose();
                        }
                    }
                    ZipStream.Finish();
                    ZipStream.Close();
                    ZipStream.Dispose();
                }
                zip.Close();
                zip.Dispose();
            }
        }

        /// <summary>
        /// ZIP:解压一个zip文件
        /// </summary>
        /// <param name="ZipFile">需要解压的Zip文件（绝对路径）</param>
        /// <param name="TargetDirectory">解压到的目录</param>
        /// <param name="Password">解压密码</param>
        /// <param name="OverWrite">是否覆盖已存在的文件</param>
        public static void UnZip(string ZipFile, string TargetDirectory, string Password, bool OverWrite = true)
        {
            //如果解压到的目录不存在，则报错
            if (!System.IO.Directory.Exists(TargetDirectory))
            {                
                throw new System.IO.FileNotFoundException("指定的目录: " + TargetDirectory + " 不存在!");
            }
            //目录结尾
            if (!TargetDirectory.EndsWith("\\")) { TargetDirectory = TargetDirectory + "\\"; }
            using (ZipInputStream zipfiles = new ZipInputStream(File.OpenRead(ZipFile)))
            {
                zipfiles.Password = Password;
                ZipEntry theEntry;
                while ((theEntry = zipfiles.GetNextEntry()) != null)
                {
                    string directoryName = "";
                    string pathToZip = "";
                    pathToZip = theEntry.Name;

                    if (pathToZip != "")
                        directoryName = Path.GetDirectoryName(pathToZip) + "\\";
                    string fileName = Path.GetFileName(pathToZip);
                    Directory.CreateDirectory(TargetDirectory + directoryName);
                    if (fileName != "")
                    {
                        if ((File.Exists(TargetDirectory + directoryName + fileName) && OverWrite) || (!File.Exists(TargetDirectory + directoryName + fileName)))
                        {
                            using (FileStream streamWriter = File.Create(TargetDirectory + directoryName + fileName))
                            {
                                int size = 2048;
                                byte[] data = new byte[2048];
                                while (true)
                                {
                                    size = zipfiles.Read(data, 0, data.Length);
                                    if (size > 0)
                                        streamWriter.Write(data, 0, size);
                                    else
                                        break;
                                }
                                streamWriter.Close();
                            }
                        }
                    }
                }

                zipfiles.Close();
            }
        }
    }
}
